<?php
namespace Compta\FormsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Compta\ChartsBundle\Entity\DataHandler;

/**
 * Description of RapportsController
 *
 * @package    ebook
 * @author     Valentin Clavreul <valentin.clavreul@smile.fr>
 * @version    SVN: $Id: RapportsController.php 6 2011-11-02 17:01:21Z vclavreul@gmail.com $
 */
class RapportsController extends Controller
{

    public function indexAction()
    {
        $transactionsRepository = $this->getDoctrine()->getRepository('ComptaFormsBundle:Transaction');
        $categoriesRepository = $this->getDoctrine()->getRepository('ComptaFormsBundle:Category');
        $categories = $categoriesRepository->findAll();

        $categoriesNames = array();
        foreach ( $categories as $cat ) {
            $categoriesNames[] = $cat->getName();
        }

        $transactions = $transactionsRepository->monthlySum('2011');
//        $data = $this->getMockData(5, 12);
        $data = $this->formatData($categoriesNames, $transactions);
//        var_dump($data);
        return $this->render('ComptaFormsBundle:Rapports:index.html.twig', array('data' => $data,'normalizedData'=>$data->getNormalizedData(3)));
    }

    public function formatData($categories, $transactions)
    {
        $data = new DataHandler();
        foreach ( $transactions as $month => $tran ) {
            $line = array();
            foreach ( $categories as $cat ) {
                if ( isset($tran[$cat]) )
                    $line[$cat] = $tran[$cat];
                else
                    $line[$cat] = 0;
            }
            $data->addLine($line, $month);
        }
        $data->setTitle($categories);
        $data->setColors($data->interpolateColors('#446688', '#ccccee', count($categories)));
        return $data;
    }

}